package com.xiaoyu.lanling.data;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;

import com.xiaoyu.base.app.AppContext;
import com.xiaoyu.lanling.R;

import in.srain.cube.app.init.InitializableAsync;
import in.srain.cube.util.UriUtil;

import static androidx.core.app.NotificationCompat.VISIBILITY_PRIVATE;

public class NotificationData implements InitializableAsync {

    public static final long[] VIBRATE_PATTERN = new long[]{0, 400, 200, 400};

    private static final NotificationData INSTANCE = new NotificationData();

    private NotificationData() {
    }

    public static NotificationData getInstance() {
        return INSTANCE;
    }

    @Override
    public void initiateAsync(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createGroupAndChannelForDefaultMessage();
            createGroupAndChannelForCall();
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createGroupAndChannelForDefaultMessage() {
        NotificationManager notificationManager = AppContext.getContext().getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(GroupId.DEFAULT, AppContext.getString(R.string.notification_group_name_default)));

        createNotificationChannelForDefaultMessage();
        createNotificationChannelForNotificationService();
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createGroupAndChannelForCall() {
        NotificationManager notificationManager = AppContext.getContext().getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(GroupId.CALL, AppContext.getString(R.string.call_notification_group_name)));

        createNotificationChannelForChatCall();
        createNotificationChannelForVideoCall();
        createNotificationChannelForVoiceMatchCall();
        createNotificationChannelForVideoMatchCall();
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannelForChatCall() {
        String name = AppContext.getString(R.string.call_notification_channel_name);
        String description = AppContext.getString(R.string.call_notification_channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH;

        NotificationChannel channel = new NotificationChannel(ChannelId.CALL_CHAT, name, importance);
        channel.setLockscreenVisibility(VISIBILITY_PRIVATE);
        channel.enableVibration(true);
        channel.setVibrationPattern(NotificationData.VIBRATE_PATTERN);
        channel.setDescription(description);
        channel.setGroup(GroupId.CALL);

        NotificationManager notificationManager = AppContext.getContext().getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannelForVideoCall() {
        String name = AppContext.getString(R.string.video_call_notification_channel_name);
        String description = AppContext.getString(R.string.video_call_notification_channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH;

        NotificationChannel channel = new NotificationChannel(ChannelId.CALL_VIDEO_CALL, name, importance);
        channel.setLockscreenVisibility(VISIBILITY_PRIVATE);
        channel.enableVibration(true);
        channel.setVibrationPattern(NotificationData.VIBRATE_PATTERN);
        channel.setDescription(description);
        channel.setGroup(GroupId.CALL);

        NotificationManager notificationManager = AppContext.getContext().getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannelForVoiceMatchCall() {
        String name = AppContext.getString(R.string.voice_match_notification_channel_name);
        String description = AppContext.getString(R.string.voice_match_notification_channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH;

        NotificationChannel channel = new NotificationChannel(ChannelId.CALL_VOICE_MATCH, name, importance);
        channel.setLockscreenVisibility(VISIBILITY_PRIVATE);
        channel.setSound(UriUtil.parseResourceId(AppContext.getContext(), R.raw.raw_voice_match_success), Notification.AUDIO_ATTRIBUTES_DEFAULT);
        channel.enableVibration(true);
        channel.setVibrationPattern(NotificationData.VIBRATE_PATTERN);
        channel.setDescription(description);
        channel.setGroup(GroupId.CALL);

        NotificationManager notificationManager = AppContext.getContext().getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannelForVideoMatchCall() {
        String name = AppContext.getString(R.string.video_match_notification_channel_name);
        String description = AppContext.getString(R.string.video_match_notification_channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH;

        NotificationChannel channel = new NotificationChannel(ChannelId.CALL_VIDEO_MATCH, name, importance);
        channel.setLockscreenVisibility(VISIBILITY_PRIVATE);
        channel.setSound(UriUtil.parseResourceId(AppContext.getContext(), R.raw.raw_voice_match_success), Notification.AUDIO_ATTRIBUTES_DEFAULT);
        channel.enableVibration(true);
        channel.setVibrationPattern(NotificationData.VIBRATE_PATTERN);
        channel.setDescription(description);
        channel.setGroup(GroupId.CALL);

        NotificationManager notificationManager = AppContext.getContext().getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannelForDefaultMessage() {
        String name = AppContext.getString(R.string.notification_channel_name_default);
        String description = AppContext.getString(R.string.notification_channel_description_default);
        int importance = NotificationManager.IMPORTANCE_HIGH;

        NotificationChannel channel = new NotificationChannel(ChannelId.DEFAULT_MESSAGE, name, importance);
        channel.setLockscreenVisibility(VISIBILITY_PRIVATE);
        channel.enableLights(true);
        channel.enableVibration(true);
        channel.setVibrationPattern(NotificationData.VIBRATE_PATTERN);
        channel.setDescription(description);
        channel.setGroup(GroupId.DEFAULT);

        NotificationManager notificationManager = AppContext.getContext().getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannelForNotificationService() {
        String name = AppContext.getString(R.string.notification_channel_name_notification_service);
        String description = AppContext.getString(R.string.notification_channel_description_notification_service);
        int importance = NotificationManager.IMPORTANCE_LOW;

        NotificationChannel channel = new NotificationChannel(ChannelId.NOTIFICATION_SERVICE, name, importance);
        channel.enableLights(false);
        channel.enableVibration(false);
        channel.setDescription(description);
        channel.setGroup(GroupId.DEFAULT);

        NotificationManager notificationManager = AppContext.getContext().getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

    public static class GroupId {
        public static final String DEFAULT = "notification_group_id_default";
        public static final String CALL = "notification_group_id_call";
    }

    public static class ChannelId {
        public static final String DEFAULT_MESSAGE = "channel_id_default_message";
        public static final String CALL_CHAT = "channel_id_call_chat";
        public static final String CALL_VOICE_MATCH = "channel_id_call_voice_match";
        public static final String CALL_VIDEO_CALL = "channel_id_call_video_call";
        public static final String CALL_VIDEO_MATCH = "channel_id_call_video_match";
        public static final String NOTIFICATION_SERVICE = "channel_id_notification_service";
    }

    public static class NotificationId {
        public static final int DEFAULT = 1;
        public static final int CALL_CHAT = 2;
        public static final int CALL_VOICE_MATCH = 3;
        public static final int CALL_VIDEO_CALL = 4;
        public static final int CALL_VIDEO_MATCH = 5;
        public static final int NOTIFICATION_SERVICE = 6;
    }

    public static class ChannelTag {
        public static final String CALL_CHAT = "channel_tag_call_chat";
        public static final String CALL_VOICE_MATCH = "channel_tag_call_voice_match";
        public static final String CALL_VIDEO_CALL = "channel_tag_call_video_call";
        public static final String CALL_VIDEO_MATCH = "channel_tag_call_video_match";
        public static final String NOTIFICATION_SERVICE = "channel_tag_notification_service";
    }
}
